package com.llap.exp190929.dto;

import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.List;

/**
 * Created by LLAP on 2019/10/14.
 * Copyright (c) 2019 LLAP. All rights reserved.
 */
@Data
public class PaginatedDto<T> {
    private int current;
    private long total;
    private int pageSize;
    private int totalPages;
    private List<T> data;

    public static <T> PaginatedDto<T> from(Page<T> result) {
        if (result == null) return new PaginatedDto<>();
        PaginatedDto<T> ret = new PaginatedDto<>();
        ret.setCurrent(result.getNumber() + 1);
        ret.setTotal(result.getTotalElements());
        ret.setPageSize(result.getSize());
        ret.setTotalPages(result.getTotalPages());
        ret.setData(result.getContent());
        return ret;
    }
}
